﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_DataLayer;
using Gem_Tools_Utility;

namespace Gem_Tools_Controller.Gem_Tools
{
    public class CAppointments_Controller
    {
        private void MapAppointments(DataRow p_row, CAppointments p_objDes)
        {
            DataTable p_dt = p_row.Table;

            if (p_dt.Columns.Contains("Auto_ID"))
                p_objDes.Auto_ID = CUtility.Convert_To_Int32(p_row["Auto_ID"]);

            if (p_dt.Columns.Contains("Priority"))
                p_objDes.Priority = CUtility.Convert_To_Int32(p_row["Priority"]);

            if (p_dt.Columns.Contains("Parent_ID"))
                p_objDes.Parent_ID = CUtility.Convert_To_Int32(p_row["Parent_ID"]);

            if (p_dt.Columns.Contains("Project_ID"))
                p_objDes.Project_ID = CUtility.Convert_To_Int32(p_row["Project_ID"]);

            if (p_dt.Columns.Contains("Type"))
                p_objDes.Type = CUtility.Convert_To_Int32(p_row["Type"]);

            if (p_dt.Columns.Contains("Start_Date"))
                p_objDes.Start_Date = CUtility.Convert_To_DateTime(p_row["Start_Date"]);

            if (p_dt.Columns.Contains("End_Date"))
                p_objDes.End_Date = CUtility.Convert_To_DateTime(p_row["End_Date"]);

            if (p_dt.Columns.Contains("All_Day"))
                p_objDes.All_Day = CUtility.Convert_To_Bool(p_row["All_Day"]);

            if (p_dt.Columns.Contains("Subject"))
                p_objDes.Subject = CUtility.Convert_To_String(p_row["Subject"]);

            if (p_dt.Columns.Contains("Location"))
                p_objDes.Location = CUtility.Convert_To_String(p_row["Location"]);

            if (p_dt.Columns.Contains("Description"))
                p_objDes.Description = CUtility.Convert_To_String(p_row["Description"]);

            if (p_dt.Columns.Contains("Status"))
                p_objDes.Status = CUtility.Convert_To_Int32(p_row["Status"]);

            if (p_dt.Columns.Contains("Label"))
                p_objDes.Label = CUtility.Convert_To_Int32(p_row["Label"]);

            if (p_dt.Columns.Contains("Resource_ID"))
                p_objDes.Resource_ID = CUtility.Convert_To_Int32(p_row["Resource_ID"]);

            if (p_dt.Columns.Contains("Resource_IDS"))
                p_objDes.Resource_IDS = CUtility.Convert_To_String(p_row["Resource_IDS"]);

            if (p_dt.Columns.Contains("Reminder_Info"))
                p_objDes.Reminder_Info = CUtility.Convert_To_String(p_row["Reminder_Info"]);

            if (p_dt.Columns.Contains("Recurrence_Info"))
                p_objDes.Recurrence_Info = CUtility.Convert_To_String(p_row["Recurrence_Info"]);

            if (p_dt.Columns.Contains("Percent_Complete"))
                p_objDes.Percent_Complete = CUtility.Convert_To_Int32(p_row["Percent_Complete"]);

            if (p_dt.Columns.Contains("Actual_Start_Date"))
                p_objDes.Actual_Start_Date = CUtility.Convert_To_DateTime(p_row["Actual_Start_Date"]);

            if (p_dt.Columns.Contains("Actual_End_Date"))
                p_objDes.Actual_End_Date = CUtility.Convert_To_DateTime(p_row["Actual_End_Date"]);

            if (p_dt.Columns.Contains("CustomField1"))
                p_objDes.CustomField1 = CUtility.Convert_To_String(p_row["CustomField1"]);

            if (p_dt.Columns.Contains("URL"))
                p_objDes.URL = CUtility.Convert_To_String(p_row["URL"]);

            if (p_dt.Columns.Contains("Created"))
                p_objDes.Created = CUtility.Convert_To_DateTime(p_row["Created"]);

            if (p_dt.Columns.Contains("Created_By"))
                p_objDes.Created_By = CUtility.Convert_To_String(p_row["Created_By"]);

            if (p_dt.Columns.Contains("Deleted"))
                p_objDes.Deleted = CUtility.Convert_To_Int32(p_row["Deleted"]);

            if (p_dt.Columns.Contains("Last_Updated"))
                p_objDes.Last_Updated = CUtility.Convert_To_DateTime(p_row["Last_Updated"]);

            if (p_dt.Columns.Contains("Last_Updated_By"))
                p_objDes.Last_Updated_By = CUtility.Convert_To_String(p_row["Last_Updated_By"]);

            if (p_dt.Columns.Contains("is_Feedback"))
                p_objDes.is_Feedback = CUtility.Convert_To_Bool(p_row["is_Feedback"]);

        }

        public IList<CAppointments> List_Appointments()
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_List_Appointments");

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }

        public CAppointments Get_Appointments_By_ID(int p_iID)
        {
            CAppointments v_objAppointments = null;
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_Get_Appointments_By_ID", p_iID);

                if (v_dt.Rows.Count > 0)
                {
                    v_objAppointments = new CAppointments();
                    MapAppointments(v_dt.Rows[0], v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_objAppointments;
        }

        public int Insert_Appointments(CAppointments p_objData)
        {
            int v_iRes = CConst.INT_VALUE_NULL;

            try
            {
                v_iRes = Convert.ToInt32(CSqlHelper.ExecuteScalar(CConfig.g_strGem_Tools_Conn_String, "sp_ins_Appointments",
                            p_objData.is_Feedback, p_objData.Priority, p_objData.Parent_ID, p_objData.Project_ID, p_objData.Type, p_objData.Start_Date, p_objData.End_Date, p_objData.All_Day, p_objData.Subject, p_objData.Location, p_objData.Description, p_objData.Status, p_objData.Label, p_objData.Resource_ID, p_objData.Resource_IDS, p_objData.Reminder_Info, p_objData.Recurrence_Info, p_objData.Percent_Complete, p_objData.Actual_Start_Date, p_objData.Actual_End_Date, p_objData.CustomField1, p_objData.URL, p_objData.Last_Updated_By).ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }


            return v_iRes;
        }

        public void Update_Appointments(CAppointments p_objData)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_upd_Appointments",
                            p_objData.Auto_ID, p_objData.is_Feedback, p_objData.Priority, p_objData.Parent_ID, p_objData.Project_ID, p_objData.Type, p_objData.Start_Date, p_objData.End_Date, p_objData.All_Day, p_objData.Subject, p_objData.Location, p_objData.Description, p_objData.Status, p_objData.Label, p_objData.Resource_ID, p_objData.Resource_IDS, p_objData.Reminder_Info, p_objData.Recurrence_Info, p_objData.Percent_Complete, p_objData.Actual_Start_Date, p_objData.Actual_End_Date, p_objData.CustomField1, p_objData.URL, p_objData.Last_Updated_By);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete_Appointments(int p_iID, string p_strLast_Updated_User)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_del_Appointments", p_iID, p_strLast_Updated_User);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<CAppointments> A1004_sp_sel_List_Appointments_By_Status(int p_iStatus, Boolean p_isFeedback)
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A1004_sp_sel_List_Appointments_By_Status", p_iStatus, p_isFeedback);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }

        public IList<CAppointments> A1004_sp_sel_List_Appointments_By_Staff_ID_By_Status(int p_iStaff_ID, int p_iStatus, Boolean p_isFeedback)
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A1004_sp_sel_List_Appointments_By_Staff_ID_By_Status", p_iStaff_ID, p_iStatus, p_isFeedback);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }

        public IList<CAppointments> A1004_sp_sel_List_Appointments_By_Staff_ID_And_Status(int p_iStaff_ID, Boolean p_isFeedback)
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A1004_sp_sel_List_Appointments_By_Staff_ID", p_iStaff_ID, p_isFeedback);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }

        public IList<CAppointments> A1004_sp_sel_List_Appointments_By_Staff_ID(int p_iStaff_ID)
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A1004_sp_sel_List_Appointments_By_Staff_ID", p_iStaff_ID);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }

        public IList<CAppointments> A1004_sp_sel_List_Feedback_ID(int p_iParent_ID, Boolean p_isFeedback)
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A1004_sp_sel_List_Feedback_ID", p_iParent_ID, p_isFeedback);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }

        public IList<CAppointments> A1004_sp_sel_List_Appointments_Contains_Date(int p_iStaff_ID, DateTime p_dtmStart_Date, DateTime p_dtmEnd_Date)
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A1004_sp_sel_List_Appointments_Contains_Date", p_iStaff_ID, p_dtmStart_Date, p_dtmEnd_Date);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }

        public IList<CAppointments> A1004_sp_sel_List_Appointments_By_Project_ID(int p_iProject_ID)
        {
            IList<CAppointments> v_arrAppointments = new List<CAppointments>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A1004_sp_sel_List_Appointments_By_Project_ID", p_iProject_ID);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAppointments v_objAppointments = new CAppointments();
                    MapAppointments(v_row, v_objAppointments);
                    v_arrAppointments.Add(v_objAppointments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAppointments;
        }
    }
}
